<?php
include_once "../init.inc.php";
if ($log->admincheck($_SESSION['loggedin'], "logon", "password", "useremail", $_SESSION['loggedusr']) == false){
    die(include("retry.php"));
    };
include_once "header.php";
//include_once "tbs_load.php";


//--------------------
if($_REQUEST['ok']){
    $cat = (int)$_REQUEST['cat'];
    $id = (int)$_REQUEST['id'];
    $extra = mysql_real_escape_string($_REQUEST['extra']);
    $license = mysql_real_escape_string($_REQUEST['license']);
    $title = mysql_real_escape_string($_REQUEST['title']);
    $type = mysql_real_escape_string($_REQUEST['type']);
    $text = mysql_real_escape_string($_REQUEST['text']);
    
//обновить пост
    $sql = "UPDATE posts 
            SET post_content='$text', extra='$extra', license='$license', post_title='$title', type='$type'
            WHERE ID=$id
            ";
        $result = mysql_query($sql) or die('Error, insert query failed');
//обновить категорию
    $sql = "";
      //  $result = mysql_query($sql) or die('Error, insert query failed');  

    echo "<div class=\"alert alert-success\">Запись обновлена</div>";
    
}
 //-------------------///////////////////////-----------------------------------------------------------------
function getSalesPage($id){
    
    $id = (int)$id;
    $sql = "SELECT ID, extra FROM `posts` WHERE ID=$id";
    $sales=""; $none="";
    $result = mysql_query($sql);
    /*
     *           <option selected value="1">none</option>
                 <option selected value="2">Sales Page</option> 
     */
    while ($rows = mysql_fetch_assoc($result)){
            if ($id==$rows['ID']){
                 if ($rows['extra']=="2") {
                       $sales="selected";                   
                     }
                     Else{$none="selected";}
            }
            
        }
    
    $ret.=      "<option $none value='1'>none</option>
                 <option $sales value=\"2\">Sales Page</option>";
    
    
        
        
        return $ret;
 
 
}
function getTypeLicense($id){
    
    $id = (int)$id;
    $sql = "SELECT ID, license FROM `posts` WHERE ID=$id";
    $plr=""; $mrr=""; $rr=""; $none="";
    $result = mysql_query($sql);
    /*
     *          <option selected value="PLR">PLR</option>
                <option selected value="MRR">MRR</option>
                <option selected value="RR">RR</option>   
     */
    while ($rows = mysql_fetch_assoc($result)){
            if ($id==$rows['ID']){
                 if ($rows['license']=="PLR") {
                       $plr="selected";                   
                     }ElseIf($rows['license']=="MRR"){
                       $mrr="selected";  
                     }ElseIf($rows['license']=="RR"){
                       $rr="selected";
                     }Else{$none="selected";}
            }
            
        }
    
    $ret.=      "<option $none value='none'>none</option>
                 <option $plr value=\"PLR\">PLR</option>
                 <option $mrr value=\"MRR\">MRR</option>
                 <option $rr value=\"RR\">RR</option>";
    
    
        
        
        return $ret;
 
 
}
function getTypeArticle($id){
    
    $id = (int)$id;
    $sql = "SELECT ID, type FROM `posts` WHERE ID=$id";
    $article=""; $ebook=""; $video=""; $none="";
    $result = mysql_query($sql);
    
    while ($rows = mysql_fetch_assoc($result)){
            if ($id==$rows['ID']){
                 if ($rows['type']=="Articles") {
                       $article="selected";                   
                     }ElseIf($rows['type']=="Ebooks"){
                       $ebook="selected";  
                     }ElseIf($rows['type']=="Videos"){
                       $video="selected";
                     }Else{$none="selected";}
            }
            
        }
    
    $ret.=      "<option $none value='none'>none</option>
                 <option $article value=\"Articles\">Articles</option>
                 <option $ebook value=\"Ebooks\">Ebooks</option>
                 <option $video value=\"Videos\">Videos</option>";
    
    
        
        
        return $ret;
 
}
function getCat($cat_id){
  
    $cat = array();
    $sql = "SELECT * FROM `category`";
    $result = mysql_query($sql); 
    $ret.= "<option selected value='0'>ALL</option>";
    
        while ($rows = mysql_fetch_assoc($result)){
            if ($cat_id==$rows['ID']){
                $ret.= "<option value=".$rows['ID']." selected >".$rows['cat_name']."</option>";
            }else{
           $ret.= "<option value=".$rows['ID'].">".$rows['cat_name']."</option>";
            }
            
        }
        
        return $ret;
 
}
//------------------------
if ($_REQUEST['type']){
$cat = (int)$_GET['cat'];
//$search = $_GET['search'];
$type = $_GET['type'];
$id = (int)$_GET['id'];

if ($_GET['id']){
   $id = (int)$_GET['id'];
   
   $sql = "SELECT p.ID, p.post_date, p.post_content, p.post_title, p.prem, p.words, c.cat_name
            FROM `posts` as p 
            LEFT JOIN `relations` AS r ON p.ID = r.post_id
            LEFT JOIN `category` AS c ON r.cat_id = c.ID
            WHERE p.ID = $id ";
 
   $result = mysql_query($sql) or die('Error, insert query failed');
    
   $row = mysql_fetch_assoc($result);
     $title = $row['post_title'];
     
            $text = $row['post_content'];
}

?>

    <a href="#"  onclick="history.back(); return false;" ><i class="icon-arrow-left"></i>&nbsp;Назад</a>

    <form method="post">
    <br><br><select name="cat"><? echo getCat($cat);?></select>   
    <br><select name="type"><?php echo getTypeArticle($id);?>
                
        </select>
        <br><select name="license"><?php echo getTypeLicense($id);?>
            
        </select>
        <br><select name="extra"><?php echo getSalesPage($id); ?>
               
        </select>
        <br><input class="input-xxlarge" type="text" name="title" value="<?=$title?>">
        
<div id="file-content">

    <textarea name ="text" id="text1" >
            <?php echo $text;  ?>
    </textarea>                  
</div>
        <input class="btn btn-primary" type="submit" value="ok" name="ok">
    </form>
   
         <?php //echo "<script>location.href=\"index.php?cat=$cat&amp;type=$type&amp;search=$search\";</script>"; ?>
  
 <?php
}
        include_once "footer.php"; 



?>
        
